"""
Case Type   : resource pool
Case Name   : 非sysadmin用户删除资源池
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.创建监控、运维、安全策略管理员用户和普通用户
    2.创建一个默认的资源池
    3.监控管理员用户删除资源池
    4.运维管理员用户删除资源池
    5.安全策略管理员用户删除资源池
    6.普通用户修改资源池
    7.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.合理报错, ERROR:  Permission denied to drop resource pools.
    4.合理报错
    5.合理报错
    6.合理报错
    7.清理成功
History     :
"""
import os
import unittest

from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_name = 'class_a'
        self.user = ['u_resource_pool_0040_1', 'u_resource_pool_0040_2',
                     'u_resource_pool_0040_3', 'u_resource_pool_0040_4']
        self.res_pool = 'resp_resource_pool_0040'
        self.err_info = 'ERROR:  Permission denied to drop resource pools.'

    def test_resource_pool(self):
        text = "-----step1:创建监控、运维、安全策略管理员用户和普通用户;expect:创建成功-----"
        self.log.info(text)
        sql = '''drop user if exists {0}, {1}, {2}, {3} cascade;
        create user {0} with monadmin password '{4}';
        create user {1} with opradmin password '{4}';
        create user {2} with poladmin password '{4}';
        create user {3} password '{4}';'''
        sql_res = self.pri_sh.execut_db_sql(
            sql.format(*self.user, macro.COMMON_PASSWD))
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.constant.CREATE_ROLE_SUCCESS_MSG),
                         4, f'执行失败:{text}')

        text = "-----step2:创建一个默认的资源池;expect:创建成功-----"
        self.log.info(text)
        sql = f"drop resource pool if exists {self.res_pool};" \
              f"create resource pool {self.res_pool};"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.create_resource_pool_success, sql_res,
                      f'执行失败:{text}')

        text = "-----step3:监控管理员用户修改资源池;expect:合理报错-----"
        self.log.info(text)
        sql = f"drop resource pool {self.res_pool};"
        sql_res = self.pri_sh.execut_db_sql(
            sql, f"-U {self.user[0]} -W{macro.COMMON_PASSWD}")
        self.log.info(sql_res)
        self.assertIn(self.err_info, sql_res, f'执行失败:{text}')

        text = "-----step4:运维管理员用户修改资源池;expect:合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            sql, f"-U {self.user[1]} -W{macro.COMMON_PASSWD}")
        self.log.info(sql_res)
        self.assertIn(self.err_info, sql_res, f'执行失败:{text}')

        text = "-----step5:安全策略管理员用户修改资源池;expect:合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            sql, f"-U {self.user[2]} -W{macro.COMMON_PASSWD}")
        self.log.info(sql_res)
        self.assertIn(self.err_info, sql_res, f'执行失败:{text}')

        text = "-----step6:普通用户修改资源池;expect:合理报错-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            sql, f"-U {self.user[3]} -W{macro.COMMON_PASSWD}")
        self.log.info(sql_res)
        self.assertIn(self.err_info, sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step7:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            "drop user if exists {0}, {1}, {2}, {3} cascade;"
            "drop resource pool {4};".format(*self.user, self.res_pool))
        self.log.info(drop_res)
        self.assertIn(self.constant.drop_resource_pool_success, drop_res,
                      f'执行失败:{text}')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_res,
                      f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
